package com.sc.common.pojo;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

/**
 * 自定义相应结构
 * Created by sc on 2017/10/16.
 * @author  sc
 */
@Data
public class CommonResult {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    private Integer status;
    private String msg;
    private Object data;

    private static  Logger logger = LoggerFactory.getLogger(CommonResult.class);

    public CommonResult() {
    }

    public static CommonResult build(Integer status, String msg, Object data) {
        return new CommonResult(status, msg, data);
    }

    public static CommonResult build(Integer status, String msg) {
        return new CommonResult(status, msg, null);
    }

    public CommonResult(Integer status, String msg, Object data) {
        this.data = data;
        this.status = status;
        this.msg = msg;
    }

    public CommonResult(Object data) {
        this.msg = "OK";
        this.status = 200;
        this.data = data;
    }

    public static CommonResult ok(Object data) {
        return new CommonResult(data);
    }

    public static CommonResult ok() {
        return new CommonResult(null);
    }

    /***
     * 将json转化为commonresult
     * @param jsonData
     * @param clazz
     * @return
     */
    public static CommonResult formatToPojo(String jsonData, Class<?> clazz) {

        try {
            if(clazz == null) {
                return MAPPER.readValue(jsonData, CommonResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data  = jsonNode.get("data");
            Object o = null;
            if(clazz!=null){
                if(data.isObject()){
                    o = MAPPER.readValue(data.traverse(),clazz);
                }else if(data.isTextual()){
                    o = MAPPER.readValue(data.asText(),clazz);
                }
            }
            return build(jsonNode.get("data").intValue(),jsonNode.get("msg").asText(),o);
        } catch (IOException e) {
            logger.error("json转换出错"+e.getMessage());
            return null;
        }
    }

    /***
     * 没有object的json转化
     * @param json
     * @return
     */
    public static CommonResult format(String json){
        try {
            return MAPPER.readValue(json,CommonResult.class);
        } catch (IOException e) {
            logger.error("json转换出错"+e.getMessage());
            return null;
        }
    }


    /***
     * 集合转化
      * @param json
     * @param clazz
     * @return
     */
    public static CommonResult formatToList(String json,Class<?> clazz){
        try {
            JsonNode jsonNode = MAPPER.readTree(json);
            JsonNode data =  jsonNode.get("data");
            Object o = null;
            if(data.isArray()&&data.size()>0){
                o = MAPPER.readValue(data.traverse(),MAPPER.getTypeFactory().constructCollectionType(List.class,clazz));
            }
            return build(jsonNode.get("status").intValue(),jsonNode.get("msg").asText(),o);
        } catch (IOException e) {
            logger.error("json转换出错"+e.getMessage());
            return null;
        }

    }

}
